package com.xiangban.yanglaodajie_platform.controller;

import com.xiangban.yanglaodajie_platform.aop.Permission;
import com.xiangban.yanglaodajie_platform.entity.DeliveryAddress;
import com.xiangban.yanglaodajie_platform.entity.dto.DeliveryAddressDto;
import com.xiangban.yanglaodajie_platform.service.DeliveryAddressService;
import com.xiangban.yanglaodajie_platform.utils.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/4/9 10:54
 */
@RestController
@RequestMapping("/api/pri/city")
@Api(tags = "收货地址")
public class DeliveryAddressController {
    @Autowired
    private DeliveryAddressService deliveryAddressService;

    @PostMapping("/getList")
    @ApiOperation("获取收货地址列表")
    @Permission
    public JsonData getList(@RequestBody DeliveryAddress deliveryAddress){
       return JsonData.buildSuccess( deliveryAddressService.getList(deliveryAddress),"请求成功");

    }
    @PostMapping("/address")
    @ApiOperation("添加收货地址")
    @Permission
    public JsonData address(@RequestBody DeliveryAddress deliveryAddress){
        int rows = deliveryAddressService.address(deliveryAddress);
        return rows == 1 ? JsonData.buildSuccess(deliveryAddress.getId(),"主键返回") : JsonData.buildError("添加失败");
    }

    @PostMapping("/deleteAddress")
    @ApiOperation("删除地址")
    @Permission
    public JsonData deleteAddress(@RequestBody DeliveryAddress deliveryAddress){
        int i = deliveryAddressService.deleteAddress(deliveryAddress);
        return i == 1 ? JsonData.buildSuccess("操作成功") :JsonData.buildError("删除失败");
    }

    @PostMapping("/updateAddress")
    @ApiOperation("修改收货地址")
    @Permission
    public JsonData updateAddress(@RequestBody DeliveryAddress deliveryAddress){
        int i = deliveryAddressService.updateAddress(deliveryAddress);
        return i == 1 ? JsonData.buildSuccess("操作成功") :JsonData.buildError("操作失败");
    }

    @PostMapping("/findObjectById")
    @ApiOperation("根据地址id获取对象")
    @Permission
    public JsonData findObjectById(@RequestBody DeliveryAddress deliveryAddress){
        DeliveryAddressDto object = deliveryAddressService.findObject(deliveryAddress);
        return object != null ? JsonData.buildSuccess(object,"获取成功"):JsonData.buildError("查不到");
    }

}
